active_sites=[1 2 4 7 8];
passive_sites=[3 5 6 9];


xc_step=[100:100:7500];


for site_id=1:9
    site_id
    m_act=[];
    all_act=[];
    for t_point=1:7
        all_act(:,:,t_point)=[proj_meta(site_id).rd(1,t_point).act(:,1:15000);proj_meta(site_id).rd(2,t_point).act(:,1:15000);proj_meta(site_id).rd(3,t_point).act(:,1:15000);proj_meta(site_id).rd(4,t_point).act(:,1:15000)];
        m_act(t_point,:)=[mean(proj_meta(site_id).rd(1,t_point).act,2);mean(proj_meta(site_id).rd(2,t_point).act,2);mean(proj_meta(site_id).rd(3,t_point).act,2);mean(proj_meta(site_id).rd(4,t_point).act,2)];
    end
    avg_act(site_id,:)=mean(m_act,2);
    fb_act=squeeze(mean(all_act(:,1:7500,:),2));
    pb_act=squeeze(mean(all_act(:,7501:15000,:),2));
    
    for ind=1:length(xc_step)
        act1=squeeze(mean(all_act(:,1:xc_step(ind),:),2));
        act2=squeeze(mean(all_act(:,xc_step(ind)+1:2*xc_step(ind),:),2));
        for t_point=1:7
            tmp=corrcoef(act1(:,t_point),act2(:,t_point));
            r_xc(ind,t_point,site_id)=tmp(2);
        end
    end
    
    for t_point=1:7
        tmp=corrcoef(fb_act(:,t_point),pb_act(:,t_point));
        fb_pb_xcorr(t_point,site_id)=tmp(2);
    end
    
%     figure;imagesc(corrcoef(m_act'));
    
%     set(gca,'clim',[0 1])
    
    cc_all(:,:,site_id)=corrcoef(m_act');
end

figure;imagesc(mean(cc_all(:,:,active_sites),3));
set(gca,'clim',[0 1]); colorbar
title('t-correlations active')

figure;imagesc(mean(cc_all(:,:,passive_sites),3));
set(gca,'clim',[0 1]); colorbar
title('t-correlations passive')


figure;hold on
plot(mean(avg_act(active_sites,:)),'r')
plot(mean(avg_act(passive_sites,:)),'k')
title('Average activity')


figure;hold on
plot(fb_pb_xcorr(:,active_sites),'r')
plot(mean(fb_pb_xcorr(:,active_sites),2),'r','linewidth',3)
plot(fb_pb_xcorr(:,passive_sites),'k')
plot(mean(fb_pb_xcorr(:,passive_sites),2),'k','linewidth',3)
title('fb-pb correlations, k-passive, r-active')


figure;hold on
plot(xc_step/15,mean(mean(r_xc(:,:,active_sites),2),3),'r')
plot(xc_step/15,mean(mean(r_xc(:,:,passive_sites),2),3),'k')
title('correlation time constant')


















